package de.slothsoft.wasbinich.swingclient.control;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import de.slothsoft.commons.swing.JToolBarManager;
import de.slothsoft.wasbinich.swingclient.control.TitlePanel.ScreenSwitcher;
import de.slothsoft.wasbinich.swingclient.res.Images;

public abstract class ChildPanel extends JPanel implements SwitchPanel {

    private static final long serialVersionUID = -3214121867216199396L;

    private static final int BORDER = 15;

    private final ScreenSwitcher screenSwitcher;
    private final JLabel titleLabel;

    private JToolBar toolBar;

    public ChildPanel(ScreenSwitcher screenSwitcher) {
	this.screenSwitcher = screenSwitcher;
	setOpaque(false);
	setLayout(new BorderLayout());
	JPanel northPanel = new JPanel();
	northPanel.setOpaque(false);
	northPanel.setLayout(new BorderLayout());
	this.titleLabel = createTitleLabel();
	if (this.titleLabel != null) {
	    northPanel.add(this.titleLabel, BorderLayout.NORTH);
	}
	northPanel.add(createToolBar(), BorderLayout.SOUTH);
	add(northPanel, BorderLayout.NORTH);
	add(createControls(), BorderLayout.CENTER);
	setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER, BORDER));
    }

    protected boolean isTitleLabelShown() {
	return true;
    }

    private JLabel createTitleLabel() {
	if (!isTitleLabelShown())
	    return null;
	JLabel result = new JLabel();
	result.setFont(new Font(getFont().getFontName(), Font.BOLD, 16));
	result.setOpaque(false);
	result.setBorder(BorderFactory.createEmptyBorder(0, 0, BORDER, 0));
	return result;
    }

    private Component createToolBar() {
	JToolBarManager manager = new JToolBarManager();
	manager.getToolBar().setFloatable(false);
	fillToolbar(manager);
	this.toolBar = manager.getToolBar();
	return manager.getToolBar();
    }

    protected void fillToolbar(JToolBarManager manager) {
	manager.add(Images.get(Images.TOOLBAR_BACK), new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		switchBack();
	    }
	});
    }

    protected void switchBack() {
	getScreenSwitcher().switchScreen(MainPanel.SCREEN_TITLE);
    }

    protected abstract Component createControls();

    @Override
    public void showPanel() {
	// nothing to do
    }

    @Override
    public void hidePanel() {
	// nothing to do
    }

    protected ScreenSwitcher getScreenSwitcher() {
	return this.screenSwitcher;
    }

    public void setTitle(String title) {
	if (this.titleLabel != null) {
	    this.titleLabel.setText(title);
	}
    }

    public String getTitle() {
	if (this.titleLabel != null)
	    return this.titleLabel.getText();
	return null;
    }

    protected JToolBar getToolBar() {
	return this.toolBar;
    }
}
